package com.managertrade.controller;

import com.managertrade.common.Result;
import com.managertrade.dto.PredictRecordDTO;
import com.managertrade.dto.TrainResponseDTO;
import com.managertrade.dto.vo.BatchDTO;
import com.managertrade.dto.vo.PredictBatchStatsVO;
import com.managertrade.dto.vo.TrainHistoryResponseVO;
import com.managertrade.dao.mysql.entity.PredictRecord;
import com.managertrade.service.impl.PredictRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 人工训练预测结果相关
 */
@RestController
@RequestMapping("api/predict")
public class PredictRecordController extends BaseController{

    @Autowired
    private PredictRecordService service;

    @PostMapping("/save")
    public Result<Void> save(@RequestBody PredictRecordDTO record) {
        record.setUserId(getUserId());
        service.save(record);
        return Result.success();
    }

    @GetMapping("/batch/{name}")
    public Result<List<PredictRecord>> getByBatch(@PathVariable String name) {
        return Result.success(service.getByBatch(name));
    }

    /**
     * 获取所有不同的批次列表（批次ID + 批次名称）
     */
    @GetMapping("/batches")
    public Result<List<BatchDTO>> getBatchList() {
        return Result.success(service.findDistinctBatches(getUserId()));
    }


    @GetMapping("/records")
    public Result<Page<PredictRecordDTO>> getPredictRecords(
            @RequestParam int page,
            @RequestParam int size,
            @RequestParam(required = false) String batchId) {

        Page<PredictRecordDTO> records = service.getPredictRecords(page, size, batchId);
        return Result.success(records);
    }

    @GetMapping("/record/statistics/{batchId}")
    public Result<PredictBatchStatsVO> getBatchStatistics(@PathVariable String batchId) {
        return Result.success(service.getBatchStatistics(batchId));
    }

    @DeleteMapping("/deletebatch/{batchId}")
    public Result<Void> deleteBatch(@PathVariable String batchId) {
        service.deleteByBatchId(batchId);
        return Result.success();
    }


    @GetMapping("/record/statistics/getTrainHistory")
    public Result<List<TrainHistoryResponseVO.HistoryItem>> getTrainHistory() {
        return Result.success(service.getTrainHistory());
    }


    @GetMapping("/record/statistics/latestTrain")
    public Result<TrainResponseDTO> latestTrain() throws Exception {
        return Result.success(service.latestTrain());
    }



}
